package common.stringTest;

public class EditString {
    public static void main(String[] args) {
        String s1 = joinAddress(null, "张家界是市", null, "-");
        System.out.println(s1);
        /*
         * public static String valueOf(Object obj) {
         *         return (obj == null) ? "null" : obj.toString();
         *     }
         *     字符串如果value是null，则会直接赋值"null"
         */
        String s2 = joinAddress2("", null, "柳州市", "鱼峰区");
//        System.out.println(s2);
    }

    /**
     * 如果字符串中有null  解决方式一：
     *
     */
    private static String joinAddress(String province,String city,String district,String address){
        String addressDetail = province+city+district+address;
        //有问题！ String会显示返回null
        if(addressDetail.contains("null")){
            String[] Strings = addressDetail.split("null");
            String realAddress = "";
//            System.out.println("length:"+Strings.length);
//            System.out.println(""+Strings[0]+Strings[1]);
            for (int i = 0; i < Strings.length; i++) {
                realAddress =realAddress+Strings[i];
            }
            return realAddress;
        }
        return addressDetail;
    }

    /**
     *
     * 如果字符串中有null  解决方式二：
     *
     */
    private static String joinAddress2(String province,String city,String district,String address){
        if(province==null){
            province="";
        }else if(city==null){
            city="";
        }else if(district==null){
            district="";
        }else if (address==null){
            address="";
        }
        return province+city+district+address;
    }
}
